package org.example.xsda.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import org.example.xsda.common.PageResult;
import org.example.xsda.common.Result;
import org.example.xsda.dto.StudentDTO;
import org.example.xsda.dto.StudentProfileUpdateDTO;
import org.example.xsda.service.StudentService;
import org.example.xsda.service.PdfService;
import org.example.xsda.vo.StudentVO;
import org.example.xsda.vo.StudentProfileVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 学生控制器
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private PdfService pdfService;

    @GetMapping("/page")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public Result<PageResult<StudentVO>> page(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long deptId,
            @RequestParam(required = false) Long classId,
            @RequestParam(required = false) Integer status
    ) {
        PageResult<StudentVO> pageResult = studentService.page(current, size, keyword, deptId, classId, status);
        return Result.success(pageResult);
    }

    @GetMapping("/list")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public Result<java.util.List<StudentVO>> list() {
        java.util.List<StudentVO> list = studentService.list();
        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result<StudentVO> getById(@PathVariable Long id) {
        StudentVO studentVO = studentService.getById(id);
        return Result.success(studentVO);
    }

    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    @PostMapping
    public Result<Void> add(@Validated @RequestBody StudentDTO dto) {
        studentService.add(dto);
        return Result.success();
    }

    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    @PutMapping
    public Result<Void> update(@Validated @RequestBody StudentDTO dto) {
        studentService.update(dto);
        return Result.success();
    }

    @SaCheckRole("admin")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        studentService.delete(id);
        return Result.success();
    }
    
    /**
     * 获取当前学生的个人档案（学生端使用）
     */
    @SaCheckRole("student")
    @GetMapping("/profile")
    public Result<StudentProfileVO> getMyProfile() {
        StudentProfileVO profile = studentService.getMyProfile();
        return Result.success(profile);
    }

    /**
     * 预览学生档案PDF（学生端使用）
     */
    @SaCheckRole("student")
    @GetMapping("/profile/preview-pdf")
    public void previewProfilePdf(HttpServletResponse response) {
        pdfService.previewStudentProfilePdf(response);
    }

    /**
     * 下载学生档案PDF（学生端使用）
     */
    @SaCheckRole("student")
    @GetMapping("/profile/download-pdf")
    public void downloadProfilePdf(HttpServletResponse response) {
        pdfService.generateStudentProfilePdf(response);
    }

    /**
     * 学生更新个人信息（学生端使用）
     * 学生只能修改：头像、证件照、联系电话、家庭住址
     */
    @SaCheckRole("student")
    @PutMapping("/profile/update")
    public Result<Void> updateMyProfile(@Validated @RequestBody StudentProfileUpdateDTO dto) {
        studentService.updateMyProfile(dto);
        return Result.success();
    }

    /**
     * 学生绑定家长（学生端使用）
     * 填写父母信息，系统自动创建家长账号并建立关联
     */
    @SaCheckRole("student")
    @PostMapping("/bind-parents")
    public Result<Void> bindParents(@RequestBody org.example.xsda.dto.ParentBindDTO dto) {
        studentService.bindParents(dto);
        return Result.success();
    }
}

